<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Generic model for entity class, CI AR.
 * @author hmQuan
 *
 */
class T_user extends MY_Model{

	function __construct(){
		parent::__construct();

		$this->table_name = "userinfo";
	}

	/**
	 * [validationUser] confirm is user
	 * @param  [type] $user_id   [description]
	 * @param  [type] $gcm_regid [description]
	 * @return array
	 */
	public function validationUser($user_id, $gcm_regid){
		return parent::validationUser($user_id, $gcm_regid);
	}

	/**
	 * get user by id
	 * @param  string  $select <option>: fields of table need get : 'st_UserID, st_Password' 
	 * @param  integer $id     id of user
	 * @return array|null
	 */
	public function get_data_by_id($id = -1, $select = '*'){
		// check id rule
		if(!is_numeric($id) || $id == -1){
			return null;	
		}

		// query
		$this->db->where(USER_ID ." = $id LIMIT 1");
		if(strcmp($select, '*') == 0){
			$this->db->select();
		}else{
			$this->db->select($select);
		}

		$result_query = $this->db->get($this->table_name);

		//convert to array
		$result_query = $result_query->result_array();

		return $result_query;
	}

	/**
	 * get data by property
	 * @param  string $select <option>: fields of table need get
	 * @param  array  $where  conditions
	 * @return array|null
	 */
	public function get_data_by_property($where = array(), $select = '*'){
		// check condition
		if($where == null || !is_array($where)){
			return null;
		}

		if(strcmp($select, '*') == 0){
			$this->db->select();
		}else{
			$this->db->select($select);
		}

		// query
		$result = $this->db->get_where($this->table_name, $where);
		// convert to array
		$result = $result->result_array();

		// return result
		return $result;
	}

	/**
	 * insert data into talbe
	 * @param array   $data   array data insert
	 * @param integer $result if 0 return id inserted else 1 return data user inserted
	 * @return null|integer|array
	 */
	public function set_data($data = array(), $result = 0){
		if($data == null || !is_array($data)){
			return null;
		}

		// insert data
		$this->db->insert($this->table_name, $data);

		// get last id
		$last_id = $this->db->insert_id();
		//return result
		if($result == 0){
			return $last_id;
		}else{
			return $this->get_data_by_id($last_id);
		}
	}

	/**
	 * update data by id
	 * @param  array   $data : data update
	 * @param  integer $id
	 * @return integer
	 */
	public function update_data_by_id($data = array(), $id = -1){
		if($data == null || !is_array($data) || !is_numeric($id) || $id == -1){
			return null;
		}

		// condition
		$this->db->where(USER_ID, $id);
		// update 
		$this->db->update($this->table_name, $data);
		
		if($this->db->affected_rows() > 0){
			return 1;
		}else{
			return null;
		}
	}

	/**
	 * update data
	 * @param  array  $data
	 * @param  array  $where : conditions update
	 * @return integer
	 */
	public function update_data_by_property($data = array(), $where = array()){
		if($data == null || !is_array($data) || $where == null || !is_array($where)){
			return null;
		}

		//get data by condition where
		$this->db->where($where);

		//update data
		$this->db->update($this->table_name, $data);

		//return result
		if($this->db->affected_rows() > 0){
			return 1;
		}else{
			return null;
		}
	}

	/**
	 * get user in gcm
	 * @param  array  $data [description]
	 * @return integer
	 */
	public function validation_user_gcm($gcm_regid = null, $user_id = null){
		if($user_id == null || !is_numeric($user_id) || $gcm_regid == null){
			return null;
		}

		$this->db->select();
    	$this->db->from($this->table_name);
    	$data = array(T_GCMUSER.'.'.GCM_REGID => $gcm_regid, $this->table_name.'.'.LOGIN_USER_ID => $user_id);
    	$this->db->where($data);
    	$this->db->join(T_GCMUSER, T_USERS.'.'.USER_ID.' = '.T_GCMUSER.'.'.USER_ID , 'inner' ) ;
    	$query_result = $this->db->get();
    	$query_result = $query_result->result_array();
    	// var_dump($query_result);exit();
    	return $query_result;
	}

	public function get_all_gcm($user_id = null){
		if($user_id == null || !is_numeric($user_id)){
			return null;
		}

		$this->db->select();
    	$this->db->from($this->table_name);
    	$data = array(T_USERS.'.'.USER_ID => $user_id);
    	$this->db->where($data);
    	$this->db->join(T_GCMUSER, T_USERS.'.'.USER_ID.' = '.T_GCMUSER.'.'.USER_ID) ;
    	$query_result = $this->db->get();
    	$query_result = $query_result->result_array();

    	return $query_result;
	}
}